var e = getApp(),
    n = e.requirejs("core"),
    e = getApp(),
    o =  e.requirejs("core");
e.requirejs("wxParse/wxParse"), e.requirejs("biz/diypage"), e.requirejs("jquery");

Page({
    data: {
        avatar: "",
        nickname: "",
        mobile: "",
        showMask: false
    },
    requestGetPhone: function (a, i) {
        n.post("wxapp/getmobile", {
            encryptedData: a.detail.encryptedData,
            iv: a.detail.iv,
            sessionkey: i
        }, function (a) {
            if ("" == a) return wx.login({
                success: function (a) {
                    n.post("wxapp/login", {
                        code: a.code
                    }, function (a) {
                        a.error ? t.alert("获取用户登录态失败:" + a.message) : e.setCache("login_session_key", a.session_key);
                    });
                },
                fail: function () {
                    n.alert("获取用户手机号信息失败!");
                },
                complete: function () {
                    wx.hideLoading();
                }
            }), void n.alert("手机号走丢了！请重新获取。");
            wx.hideLoading(), wx.navigateTo({
                url: "/pages/member/bind/new_index?mobile=" + a
            });
        });
    },
    getPhoneNumber: function (a) {
        var i = this;
        a.detail.iv && (wx.showLoading({
            title: "加载中"
        }), wx.checkSession({
            success: function () {
                var t = e.getCache("login_session_key");
                console.log("sessionKey:" + t), i.requestGetPhone(a, t);
            },
            fail: function () {
                wx.login({
                    success: function (n) {
                        t.post("wxapp/login", {
                            code: n.code
                        }, function (n) {
                            n.error ? t.alert("获取用户登录态失败:" + n.message) : (e.setCache("login_session_key", n.session_key),
                                i.requestGetPhone(a, n.session_key));
                        });
                    },
                    fail: function () {
                        t.alert("获取用户手机号信息失败!");
                    },
                    complete: function () {
                        wx.hideLoading();
                    }
                });
            }
        }));
    },
    onLoad: function (e) {
        console.log(e), this.setData({
            avatar: e.avatar,
            nickname: e.nickname
        });
        this.userinfo()
    },
    userinfo: function () {
        let p = this
        n.get("member", {}, function (t) {
            p.setData({
                levelname: t.levelname
            })
        })
    },
    // 显示修改头像
    showMyMask() {
        // console.log('显示修改的内容')
        this.setData({
            showMask: true
        })
    },
    // 上传 用户信息
    uploadUserInfo() {
        let that = this
        if (that.data.nickname == '') return wx.showToast({
            title: '请填写昵称',
            icon: 'none'
        })
        if (that.data.avatar == '') return wx.showToast({
            title: '请选择头像',
            icon: 'none'
        })
        o.get('wxapp/setprofile', {
            avatar: that.data.avatar,
            nickname: that.data.nickname
        }, function (res) {
            console.log(res)
            let userinfo = e.getCache("userinfo")
            userinfo.avatarUrl = that.data.headPortrait
            userinfo.nickName = that.data.name
            userinfo.openid = res.openId
            userinfo.id = res.id
            userinfo.uniacid = res.uniacid;
            // console.log(222222, userinfo);
            e.setCache("userinfo", userinfo)
            wx.setStorageSync('userinfo', res)
            wx.showToast({
                title: '修改成功',
            })
            that.maskBtn()
        })

    },



    // 获取头像
    onChooseAvatar(e) {
        console.log(e)
        let that = this
        that.setData({
            avatar: e.detail.avatarUrl
        })
        wx.uploadFile({
            url: 'https://zzsl.sctqmt.com/app/ewei_shopv2_api.php?i=1&r=util.uploader.upload',
            filePath: e.detail.avatarUrl,
            name: 'file',
            success: function (res) {
                // console.log(res)
                console.log(JSON.parse(res.data))
                that.setData({
                    avatar: JSON.parse(res.data).files[0].url
                })
                // console.log(that.data.headPortrait)
            }
        })
    },

    // 获取名称
    getName(e) {
        this.setData({
            nickname: e.detail.value
        })
    },
    maskBtn(){
        this.setData({
            showMask: false
        })
        // 关闭后处理问题
       
    },
    onReady: function () {},
    onShow: function () {
        var e = this;
        n.get("member", {}, function (n) {
            e.setData({
                mobile: n.mobile
            });
        });
    },
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    onShareAppMessage: function () {}
});